package com.gy.api.pc.clap.controller;

//import cn.afterturn.easypoi.excel.ExcelExportUtil;
//import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.github.pagehelper.PageInfo;
import com.gy.api.base.ApiIndex;

import com.gy.api.base.Constants;
import com.gy.api.base.R;
import com.gy.api.base.RestPage;
import com.gy.api.pc.clap.domain.vo.*;
import com.gy.api.pc.clap.service.IAssociationClapCulvertService;
import com.gy.api.pc.examine.domain.vo.ExamineCompleteVO;
import com.gy.api.pc.examine.domain.vo.ExamineLetterVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.net.URLEncoder;


@RestController
@RequestMapping("/association/clap/culvert")
@RequiredArgsConstructor
@Api( tags = ApiIndex.AssociationClapCulvertController)
public class AssociationClapCulvertController {

    private final IAssociationClapCulvertService associationClapCulvertService;


    @GetMapping("/approval-page")
    @ApiOperation(value = "协拍涵办理申请列表-全部")
    public R<RestPage<List<PageCulvertApprovalVO>>> getApprovalPage( @ApiParam(value = "查询参数") @ModelAttribute PageCulvertSearchVO searchVO) {
        searchVO.setType(null);
        List<PageCulvertApprovalVO> list = associationClapCulvertService.getApprovalPage(searchVO);
        RestPage restPage = new RestPage( new PageInfo(list),list);
        return new R(restPage);
    }

    @GetMapping("/acceptance-page")
    @ApiOperation(value = "协拍涵办理申请【受理】列表")
    public R<RestPage<List<PageCulvertApprovalVO>>> getAcceptancePage( @ApiParam(value = "查询参数") @ModelAttribute PageCulvertSearchVO searchVO) {
        searchVO.setApprovalUserId(StpUtil.getLoginIdAsString());
        searchVO.setNodeName(Constants.EXAMINE_COOPERATION_LETTER[1]);
        List<PageCulvertApprovalVO> list = associationClapCulvertService.getApprovalPage(searchVO);
        RestPage restPage = new RestPage( new PageInfo(list),list);
        return new R(restPage);
    }

    @GetMapping("/reviewDecision-page")
    @ApiOperation(value = "协拍涵办理申请【审查决定】列表")
    public R<RestPage<List<PageCulvertApprovalVO>>> getReviewDecisionPage( @ApiParam(value = "查询参数") @ModelAttribute PageCulvertSearchVO searchVO) {
        searchVO.setApprovalUserId(StpUtil.getLoginIdAsString());
        searchVO.setNodeName(Constants.EXAMINE_COOPERATION_LETTER[2]);
        List<PageCulvertApprovalVO> list = associationClapCulvertService.getApprovalPage(searchVO);
        RestPage restPage = new RestPage( new PageInfo(list),list);
        return new R(restPage);
    }

    @GetMapping("/stampSendLetter-page")
    @ApiOperation(value = "协拍涵办理申请【盖章发函】列表")
    public R<RestPage<List<PageCulvertApprovalVO>>> getStampSendLetterPage( @ApiParam(value = "查询参数") @ModelAttribute PageCulvertSearchVO searchVO) {
        searchVO.setApprovalUserId(StpUtil.getLoginIdAsString());
        searchVO.setNodeName(Constants.EXAMINE_COOPERATION_LETTER[3]);
        List<PageCulvertApprovalVO> list = associationClapCulvertService.getApprovalPage(searchVO);
        RestPage restPage = new RestPage( new PageInfo(list),list);
        return new R(restPage);
    }


    @GetMapping("/approval-detail")
    @ApiOperation(value = "协拍涵办理申请-详情")
    public R<CulvertDetailVO> getApprovalDetail(@RequestParam String id) {
        CulvertDetailVO culvertVo = associationClapCulvertService.getApprovalDetail(id);
        return new R(culvertVo);
    }

    @ApiOperation("完成审批/受理")
    @PostMapping("/complete")
    @SaCheckLogin
    public R complete(@RequestBody ExamineCompleteVO taskCompleteDto){
        associationClapCulvertService.completeTask(taskCompleteDto);
        return new R();
    }


    @ApiOperation("盖章发函")
    @PostMapping("/complete_letter")
    @SaCheckLogin
    public R complete_letter(@RequestBody ExamineLetterVO taskCompleteDto){
        associationClapCulvertService.completeLetter(taskCompleteDto);
        return new R();
    }





//    @GetMapping("/page")
//    @ApiOperation(value = "协拍涵管理列表 ")
//    public R<RestPage<List<PageCulvertVo>>> pageCulvert(@ModelAttribute PageCulvertDto dto) throws Exception {
//        List<PageCulvertVo> list = associationClapCulvertService.pageCulvert(dto);
//        RestPage restPage = new RestPage( new PageInfo(list),list);
//        return new R(restPage);
//    }
//
//    @GetMapping("/feedback")
//    @ApiOperation(value = "协拍涵管理-反馈协调情况/详情")
//    public R<List<TAssociationClapDetail>> feedbackCulvert(@RequestParam String id) throws Exception {
//        List<TAssociationClapDetail> list = associationClapCulvertService.feedbackCulvert(id);
//        return new R(list);
//    }
//
//    @PostMapping("/harmonize")
//    @ApiOperation(value = "协拍涵管理-反馈协调情况-协调接口/单个协调/全部协调")
//    public R<String> harmonizeCulvert(@Validated @RequestBody CulvertHarmonizeDto dto) throws Exception {
//        List<String> ids = dto.getIds();
//        associationClapCulvertService.harmonizeCulvert(ids);
//        return new R();
//    }
//
    @PostMapping("/export")
    @ApiOperation(value = "协拍涵清单导出")
    public void export(@Validated @RequestBody CulvertHarmonizeDto dto, HttpServletResponse response) throws IOException {
        List<CulvertApprovalDetailExportVo> export = associationClapCulvertService.export(dto);
        // 导出参数设置
        ExportParams exportParams = new ExportParams("重庆市影视拍摄协调服务函", "协拍涵");

        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, CulvertApprovalDetailExportVo.class, export);
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("UTF-8");
        String fileName = URLEncoder.encode("culvert_approval_detail.xlsx", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename*=UTF-8''" + fileName);
        try (ServletOutputStream out = response.getOutputStream()) {
            workbook.write(out);
            out.flush();
        } catch (IOException e) {
//            e.printStackTrace();
        } finally {
            if (workbook != null) {
                try {
                    ((AutoCloseable) workbook).close();
                } catch (Exception ex) {
                    // 忽略关闭异常
                }
            }

        }

    }

    @GetMapping("/export_xlsx")
    @ApiOperation(value = "协拍涵清单导出")
    public void export_xlsx(@Validated @ModelAttribute CulvertHarmonizeDto dto, HttpServletResponse response) throws IOException {
        List<CulvertApprovalDetailExportVo> export = associationClapCulvertService.export(dto);
        // 导出参数设置
        ExportParams exportParams = new ExportParams("重庆市影视拍摄协调服务函", "协拍涵");

        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, CulvertApprovalDetailExportVo.class, export);
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("UTF-8");
        String fileName = URLEncoder.encode("场景清单-"+dto.getName()+".xlsx", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename*=UTF-8''" + fileName);
        try (ServletOutputStream out = response.getOutputStream()) {
            workbook.write(out);
            out.flush();
        } catch (IOException e) {
//            e.printStackTrace();
        } finally {
            if (workbook != null) {
                try {
                    ((AutoCloseable) workbook).close();
                } catch (Exception ex) {
                    // 忽略关闭异常
                }
            }

        }

    }
}